package cevent.source.cloudcenter.system.controller.admin;

import cevent.source.cloudcenter.server.dto.SiteSingleDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.SiteSingleService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 独立信息设置表控制层
 */
@RestController
@RequestMapping("/admin/siteSingle")
public class SiteSingleController {
    private static final Logger LOG = LoggerFactory.getLogger(SiteSingleController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 独立信息设置表";
    @Resource
    private SiteSingleService siteSingleService;

    //1.dto-siteSingle获取
    @RequestMapping("/dtoList")
    public List<SiteSingleDto> getDtoList() {
        return siteSingleService.siteSingleDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        siteSingleService.getSiteSinglePageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody SiteSingleDto siteSingleDto) {
        LOG.info("传入的siteSingle DTO：{}", siteSingleDto);

        ValidatorUtil.requiredLength(siteSingleDto.getName(), "独立信息名称", 3, 255);
        ValidatorUtil.requiredLength(siteSingleDto.getTag(), "独立信息标签", 3, 255);
        ResponseDataDto responseData = new ResponseDataDto();
        siteSingleService.save(siteSingleDto);
        responseData.setResponseData(siteSingleDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的siteSingle uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        siteSingleService.delete(uniId);
        return responseData;
    }
}
